/**
 * 2017年5月2日
 */
package util.tags;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;

import cn.edu.bjtu.classification.ClassificationResult;
import cn.edu.bjtu.constant.Constant;
import cn.edu.bjtu.entity.ExecuteResult;
import cn.edu.bjtu.entity.ViewRenderExecuteResult;

/**
 * @author Alex
 *
 */
public class MapResultRenderTag extends BaseTag{
	private String dataToRender;
	/**
	 * @param dataToRender the dataToRender to set
	 */
	public void setDataToRender(String dataToRender) {
		this.dataToRender = dataToRender;
	}

	private final static String htmls=
			  "<div>"
			+ "<p>服务器执行方式:%s</p>"
			+ "<p class='%s'>执行结果:%s</p>"
			+ "<p>远程服务器提示:%s</p>"
			+ "<div>%s</div>"
			+ "</div>";
	JspWriter out;
	public void doTag() throws JspException,IOException{
		out = getJspContext().getOut();
		Object o = (this.dataToRender==null||this.dataToRender.length()==0)? this.request.getAttribute(Constant.JSP_MAP_RENDER_ATTRIBUTE):this.request.getAttribute(this.dataToRender);
		if(o!=null && o instanceof ViewRenderExecuteResult){
			doViewRenderExecuteResult(o);
		}else if(o!=null && o instanceof ClassificationResult){
			doClassificationResult((ClassificationResult)o);
		}else if(o!=null && o instanceof ExecuteResult){
			doExecuteResult((ExecuteResult)o);
		}else{
			out.println("It seems that you put a wrong type for this tag to display");
		}
		
	}
	private void doExecuteResult(ExecuteResult o) throws IOException{
		Object param1 = o.isAsync()?"异步":"同步";
		Object param2 = o.isSuccess()?"成功":"失败";
		Object param3 = o.getTipMessage();
		out.println(String.format(htmls, param1,o.isSuccess()?"text-success":"text-danger",param2,param3,o.isSuccess()?"执行成功":"执行失败"));
	}
	
	private void doClassificationResult(ClassificationResult o) throws IOException{
		String s = o.toClassLabelDescription();
		out.println(s);
	}
	
	private void doViewRenderExecuteResult(Object o) throws IOException{
		ViewRenderExecuteResult vrer = (ViewRenderExecuteResult) o;
		Object param1 = vrer.isAsync()?"异步":"同步";
		Object param2 = vrer.isSuccess()?"成功":"失败";
		Object param3 = vrer.getTipMessage();
		Map<String,Object> map = vrer.getResultMapForRender();
		if(map!=null){
		//linkedhashmap可以保持数据插入的顺序
			StringBuilder sb = new StringBuilder(100);
			sb.append("<table class='table table-bordered'>");
				for(Iterator it = map.keySet().iterator();it.hasNext();){
					Object key = it.next();
					sb.append("<tr>");
					sb.append("<td>");
					sb.append(key.toString().equals("tip")?"WEB服务器提示":key.toString());
					sb.append("</td>");
					sb.append("<td>");
					sb.append(map.get(key));
					sb.append("</td>");
					sb.append("</tr>");
				}	
			sb.append("</table>");
			out.println(String.format(htmls, param1,vrer.isSuccess()?"text-success":"text-danger",param2,param3,sb.toString()));
		}else{
			out.println(String.format(htmls, param1,vrer.isSuccess()?"text-success":"text-danger",param2,param3,null));
		}
	}
	
}
